package games.platformer.behaviors;

import java.util.HashSet;
import java.util.Set;

import vooga.core.AbstractLevel;
import vooga.entities.Entity;


/**
 * 
 * adapted by Jonathan Odom for Vooga
 * 
 * @author Robert C. Duvall
 */
public abstract class Intersect extends vooga.behaviors.AbstractBehavior
{
    protected vooga.geom.Vector myVelocity;
    private Set<Entity> myObstacles;
    //store obstacles to add or remove until next step so we don't get
    //concurrent modification exceptions
    private Set<Entity> myObstaclesToAdd;
    private Set<Entity> myObstaclesToRemove;
    private AbstractLevel myLevel;

    public Intersect (vooga.geom.Vector velocity, vooga.entities.Entity obstacle)
    {
        this(velocity);
        add(obstacle);
    }

    public Intersect (vooga.geom.Vector velocity, java.util.List<Entity> obstacles)
    {
        this(velocity);
        addAll(obstacles);
    }
    
    public Intersect (vooga.geom.Vector velocity, java.util.Collection<Entity> obstacles)
    {
    	this(velocity);
    	addAll(obstacles);
    }

    protected Intersect (vooga.geom.Vector velocity)
    {
        myObstacles = new java.util.HashSet<vooga.entities.Entity>();
        myVelocity = velocity;
        myObstaclesToAdd = new HashSet<Entity>();
        myObstaclesToRemove = new HashSet<Entity>();
    }


    public void step (vooga.entities.Entity target)
    {
    	
    	for(Entity e : myObstaclesToAdd)
    		myObstacles.add(e);
       	myObstaclesToAdd.clear();

       	for(Entity e : myObstaclesToRemove)
        	myObstacles.remove(e);
       	myObstaclesToRemove.clear();
       	
    	for(Entity e : myObstacles)
        	if(target.intersects(e))
        		onCollide(target, e);
        
    }


    public void add(Entity a)
    {
    	myObstaclesToAdd.add(a);
    }
    
    public void addAll(java.util.Collection<Entity> e)
    {
    	myObstaclesToAdd.addAll(e);
    }
    
    public boolean remove(Entity a)
    {
    	return myObstaclesToRemove.add(a);
    }
    

    protected abstract void onCollide (vooga.entities.Entity lhs, vooga.entities.Entity rhs);
}
